/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright (c) 2010 by Emiliano Pereyra.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 */
package limfic.persistencia;

import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Timestamp;
import java.sql.SQLException;

/**
 * Colección de utilidades para el acceso a datos a travéz de JDBC.
 * 
 * <p>La sobrecarga del método <code>close()</code> de esta clase elimina la 
 * necesidad de utilizar un try/catch engorroso para manejar las SQLException 
 * disparadas por los respectivos métodos <code>close()</code> de las interfases
 * Connection, Statemente, PreparedStatement y CallableStatement.
 * 
 * @author emmie
 */
public class UtilidadesDB {

    /**
     * Cierra un PrepareStatement y loguea la SQLException en caso que ocurra.
     * 
     * @param pStmt PrepareStatement a cerrar
     */
    public static void close(PreparedStatement pStmt) {
        if (pStmt == null) {
            return;
        }
        try {
            pStmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * Cierra un Statement y loguea la SQLException en caso que ocurra.
     * 
     * @param stmt Statement a cerrar
     */
    public static void close(Statement stmt) {
        if (stmt == null) {
            return;
        }
        try {
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * Cierra un CallableStatement y loguea la SQLException en caso que ocurra.
     * 
     * @param cStmt CallableStatement a cerrar
     */
    public static void close(CallableStatement cStmt) {
        if (cStmt == null) {
            return;
        }
        try {
            cStmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * Cierra un ResultSet y loguea la SQLException en caso que ocurra.
     * 
     * @param rs ResultSet a cerrar.
     */
    public static void close(ResultSet rs) {
        if (rs == null) {
            return;
        }
        try {
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * Cierra un Connection y loguea la SQLException en caso que ocurra.
     * 
     * @param conn Connection a cerrar.
     */
    public static void close(Connection conn) {
        if (conn == null) {
            return;
        }
        try {
            if (!conn.isClosed()) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * Convierte un java.sql.Timestamp a un java.util.Date.
     * 
     * @param Timestamp
     * @return Date
     */
    public static java.util.Date convertTimestampToUtilDate(Timestamp ts) {
        if (ts == null) {
            throw new IllegalArgumentException("No se permiten timestamps nulos.");
        }
        return new java.util.Date(ts.getTime());
    }

    /**
     * Convierte un java.util.Date a un java.sql.Timestamp.
     *
     * @param Date
     * @return Timestamp
     */
    public static Timestamp convertUtilDateToTimestamp(java.util.Date date) {
        if (date == null) {
            throw new IllegalArgumentException("Non se permiten dates nulos.");
        }
        return new Timestamp(date.getTime());
    }
}

